package com.junzhiit.nengji.cms.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.bainaeco.framework.base.BaseObject;
import com.junzhiit.nengji.cms.service.SkuItemsService;
import com.junzhiit.nengji.cms.web.exception.SkuItemsNameIsExistException;
import com.junzhiit.nengji.cms.web.exception.SkuItemsNotFoundException;
import com.junzhiit.nengji.cms.web.exception.SkuNotFoundException;
import com.junzhiit.nengji.cms.web.util.ViewObjectUtil;
import com.junzhiit.nengji.cms.web.vo.DataTablesParamVO;
import com.junzhiit.nengji.cms.web.vo.ResultVO;
import com.junzhiit.nengji.cms.web.vo.SkuItemsAddVO;
import com.junzhiit.nengji.cms.web.vo.SkuItemsEditVO;
import com.junzhiit.nengji.dao.SkuDAO;
import com.junzhiit.nengji.dao.SkuItemsDAO;
import com.junzhiit.nengji.model.Sku;
import com.junzhiit.nengji.model.SkuItems;

@Service
public class SkuItemsServiceImpl extends BaseObject implements SkuItemsService {

	private static final long serialVersionUID = 157442341972332290L;

	@Autowired
	SkuItemsDAO skuItemsDao;

	@Autowired
	SkuDAO skuDao;

	@Override
	public Map<String, Object> findAll(DataTablesParamVO dtp, String skuId) {
		List<SkuItems> list = skuItemsDao.findAll(skuId, dtp.getStart(), dtp.getLength());
		int totalCount = skuItemsDao.getTotalCount(skuId);
		return ViewObjectUtil.dataTables(list, dtp.getDraw(), totalCount);
	}

	@Transactional
	@Override
	public ResultVO add(SkuItemsAddVO skuItemsAddVO) {
		ResultVO result = new ResultVO();

		Sku sku = skuDao.query(skuItemsAddVO.getSkuId());
		if (sku == null) {
			throw new SkuNotFoundException("该顶级SKU未找到");
		}

		int count = skuItemsDao.queryNameIsExit(skuItemsAddVO.getSkuId(), skuItemsAddVO.getStandard());

		if (count > 0) {
			throw new SkuItemsNameIsExistException("该SKU规格已经存在");
		}

		SkuItems skuItems = new SkuItems();
		BeanUtils.copyProperties(skuItemsAddVO, skuItems);

		SkuItems data = skuItemsDao.insert(skuItems);
		result.setData(data);
		return result;
	}

	@Override
	public ResultVO delete(String id) {
		ResultVO result = new ResultVO();
		boolean data = skuItemsDao.updateToDelete(id);
		result.setData(data);
		return result;
	}

	@Override
	public ResultVO batchDelete(String[] ids) {
		ResultVO result = new ResultVO();
		boolean data = skuItemsDao.batchUpdateToDelete(ids);
		result.setData(data);
		return result;
	}

	@Transactional
	@Override
	public ResultVO update(SkuItemsEditVO skuItemsEditVO) {
		ResultVO result = new ResultVO();

		SkuItems skuItems = skuItemsDao.query(skuItemsEditVO.getId());
		if (skuItems == null) {
			throw new SkuItemsNotFoundException("sku规格未找到");
		}
		
		if(skuItems.getStandard().equals(skuItemsEditVO.getStandard())){
			return result;
		}
		
		int count = skuItemsDao.queryNameIsExit(skuItems.getSkuId(), skuItemsEditVO.getStandard());
		if (count > 0) {
			throw new SkuItemsNameIsExistException("该SKU规格已经存在");
		}
		skuItems.setStandard(skuItemsEditVO.getStandard());
		boolean flag = skuItemsDao.update(skuItems);

		result.setData(flag);
		return result;
	}
	
	@Override
	public ResultVO getAll(String skuId){
		ResultVO result=new ResultVO();
		List<SkuItems> data=skuItemsDao.getAll(skuId);
		result.setData(data);
		return result;
	}
	
	@Override
	public ResultVO queryGoodsAboutSkuItemsCount(String skuItemsId){
		ResultVO result=new ResultVO();
		int data=skuItemsDao.queryGoodsAboutSkuItemsCount(skuItemsId);
		result.setData(data);
		return result;
	}
}
